package com.fable.ntzw_ga.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fable.common.dto.GuestDTO;
import com.fable.common.vo.ResultVO;
import com.fable.ntzw_ga.entity.FeastType;
import com.fable.ntzw_ga.entity.Guest;
import com.fable.ntzw_ga.entity.GuestMap;
import com.fable.ntzw_ga.service.IFeastTypeService;
import com.fable.ntzw_ga.service.IGuestMapService;
import com.fable.ntzw_ga.service.IGuestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.List;


/**
 * @author Created by gzh on 2023/3/8 0:19
 */

@Api(tags = "宾客管理")
@RestController
@RequestMapping("/we_xin/guest")
@PreAuthorize("hasRole('admin')")
@Slf4j
public class WXGuestController {

    @Autowired
    IGuestService guestService;

    @Autowired
    IGuestMapService guestMapService;

    @Autowired
    IFeastTypeService feastTypeService;

    @ApiOperation(value = "搜索微信信息")
    @PostMapping("getWxAll")
    public ResultVO getDataList() {
        LambdaQueryWrapper<Guest> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Guest::getCreateTime);
        return ResultVO.okResult(guestService.list(wrapper));
    }

    @ApiOperation(value = "添加宾客")
    @PostMapping("save")
    @Transactional
    public ResultVO saveGuestUser(@RequestBody GuestDTO guestDTO) {
        FeastType one = feastTypeService.getOne(new LambdaQueryWrapper<FeastType>().eq(FeastType::getFeastStatus, true));
        Guest guest = new Guest();
        BeanUtils.copyProperties(guestDTO,guest);
        guest.setFeastTypeId(one.getId());
        guest.setCreateTime(LocalDateTime.now());
        guest.setRemarks(one.getRemarks());
        //地图经纬度
        GuestMap guestMap = guestDTO.getGuestMap();
        if (guestMap!=null){
            guestMapService.save(guestMap);
        }

        guestService.save(guest);
        return ResultVO.okResult();
    }

    @ApiOperation(value = "获取酒席类型")
    @GetMapping("getFeastTypeList")
    public ResultVO getFeastTypeList(){
        LambdaQueryWrapper<FeastType> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(FeastType::getRemarks,FeastType::getId).orderByDesc(FeastType::getFeastTime);
        List<FeastType> list = feastTypeService.list(wrapper);
        return ResultVO.okResult(list);
    }
}
